package action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import dao.StudentDAO;
import model.Student;

public class StudentAction {
	HttpServletRequest req;
	private Student student;
	private File image;
	private String imageContentType;
	private String imageFileName;
	StudentDAO dao=new StudentDAO();

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}
	
	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String add() {
		dao.update(student);
		return "main";
	}
	
	public String check() {
		Student stud=dao.check(student);
		if(stud==null) {
			return "login";			
		}
//		方法一  将用户的信息存放在session中
//		HttpServletRequest request=ServletActionContext.getRequest();
//		HttpSession session=request.getSession();
//		session.setAttribute("STUDENT", stud);

//		方法二  
		ActionContext.getContext().getSession().put("STUDENT", stud);
		return "main";
	}
	
	//判断旧密码
	public String checkpwd() throws IOException {
		String result="0";
		if(dao.checkpwd(student)) {
			result="1";
		}
		HttpServletResponse response=ServletActionContext.getResponse();

	    PrintWriter out=response.getWriter();
	    out.print(result);
		
		return null;
	}
	
	public String update() throws IOException {
		req=ServletActionContext.getRequest();
		//如果保存上传文件的根目录不存在,创建根目录
		if(image!=null) {
			String fileName="uploadfiles";
			String url=fileName;//开始图片地址
			
			fileName=req.getServletContext().getRealPath("")+fileName;
			File file=new File(fileName);
			if(!file.exists())
			{
				file.mkdir();
			}
			Calendar cal=Calendar.getInstance();
	
			//如果年的目录不存在，创建年的目录
			int year=cal.get(Calendar.YEAR);
			fileName=fileName + "\\" + year;
			url=url+ "\\" + year;
			file=new File(fileName);
			if(!file.exists())
			{
				file.mkdir();
			}
			//如果月份不存在，创建月份的目录
			int month=cal.get(Calendar.MONTH)+1;
			fileName=fileName+"\\";
			url=url+ "\\";
			if(month<10)
			{
				fileName=fileName+"0";
				url=url+"0";
			}
			fileName=fileName+month;
			url=url+month;
			file=new File(fileName);
			if(!file.exists())
			{
				file.mkdir();
			}
	
			//生成文件名的日部分
			int day=cal.get(Calendar.DAY_OF_MONTH);
			fileName=fileName+"\\";
			url=url+"\\";
			if(day<10)
			{
				fileName=fileName+"0";
				url=url+"0";
			}
			fileName=fileName+day;
			url=url+day;
			
			//生成文件名的小时部分
			int hour=cal.get(Calendar.HOUR_OF_DAY);
			if(hour<10)
			{
				fileName=fileName+"0";
				url=url+"0";
			}
			fileName=fileName+hour;	
			url=url+hour;
			//生成文件名的分钟部分
			int minute=cal.get(Calendar.MINUTE);
			if(minute<10)
			{
				fileName=fileName+"0";
				url=url+"0";
			}
			fileName=fileName+minute;		
			url=url+minute;
			
			//生成文件名的秒部分
			int second=cal.get(Calendar.SECOND);
			if(second<10)
			{
				fileName=fileName+"0";
				url=url+"0";
			}
			fileName=fileName+second;		
			url=url+second;
			
			//生成文件名的毫秒部分
			int millisecond=cal.get(Calendar.MILLISECOND);
			if(millisecond<10)
			{
				fileName=fileName+"0";
				url=url+"0";
			}
			if(millisecond<100)
			{
				fileName=fileName+"0";
				url=url+"0";
			}
				
			fileName=fileName+millisecond;
			url=url+millisecond;
			
			//生成文件的扩展名部分
			String extension= imageFileName.substring(imageFileName.indexOf(".")) ;
			fileName=fileName+extension;
			url=url+extension;
			file=new File(fileName);
			FileUtils.copyFile(image, file);//复制图片和存放的路径
			//获取之前图片的路径，不为空，删除
			String oldurl=dao.getUrl(student);
			if(oldurl!=null) {
				String oldfile=req.getServletContext().getRealPath("")+oldurl;
				File myfile=new File(oldfile);
				if(myfile.exists()) {
					myfile.delete();
				}
			}
			
			student.setPhoto(url);
		}
		dao.update(student);
		HttpSession session=req.getSession();
		session.setAttribute("STUDENT", student);
		return "main";		
	}
}
